package com.liaoban.app.adapter;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.PagerAdapter;

import com.liaoban.app.util.CommonUtils;

public class MFragmentPagerAdapter extends FragmentPagerAdapter {
    private Class<? extends Fragment>[] mClasses;
    private String[] mTitlesId;
    private Bundle[] mBundles;

    public MFragmentPagerAdapter(FragmentManager fm, Class[] classes, String[] titles, Bundle[] bundles) {
        super(fm);
        this.mClasses = classes;
        this.mTitlesId = titles;
        this.mBundles = bundles;
    }

    @Override
    public Fragment getItem(int position) {
        try {
            if (!CommonUtils.isEmpty(mClasses)) {
                Fragment fragment = mClasses[position].newInstance();
                if (!CommonUtils.isEmpty(mBundles)) {
                    fragment.setArguments(mBundles[position]);
                }
                return fragment;
            }
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int getItemPosition(Object object) {
        return PagerAdapter.POSITION_NONE;
    }

    @Override
    public int getCount() {
        if (!CommonUtils.isEmpty(mClasses)) {
            return mClasses.length;
        }
        return 0;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return mTitlesId[position];
    }
}